package com.jsyh.maidu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.ArticleWebActivity;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.model.ArticleListChild;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/7/12.
 */

public class ArticleListAdapter extends RecyclerView.Adapter<ArticleListAdapter.ArticleHolder> {

    List<ArticleListChild> datas;
    Context context;

    public ArticleListAdapter(List<ArticleListChild> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public ArticleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.article_child_list_item, null);
        ArticleHolder holder = new ArticleHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ArticleHolder holder, final int position) {
        holder.title.setText(datas.get(position).getTitle());
        holder.time.setText(datas.get(position).getAdd_time());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(context, ArticleWebActivity.class);
                it.putExtra("url", ConfigValue.APP_IP + datas.get(position).getArt_url());
                context.startActivity(it);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    class ArticleHolder extends RecyclerView.ViewHolder{

        TextView title;
        TextView time;
        View view;

        public ArticleHolder(View itemView) {
            super(itemView);
            view = itemView;
            title = (TextView) itemView.findViewById(R.id.child_article_title);
            time = (TextView) itemView.findViewById(R.id.child_article_time);
        }
    }
}
